<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;

use Think\Controller;

/**
 * 林州市悟能科技提供技术支持
 * 400-037203
 * @author Mr_Lee :672288511;
 */
class ArticleController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['username'])) {

            //$this->error('页面加载中。。。',U('Login/Login'),1);
            $this->redirect('Login/Login');

        }
        $wang=__WANG__;
        $table1="article";
        $this->assign('table',$table1);
        $this->assign('wang',$wang);
    }

    //文章列表
    public function lst()
    {   
        $count=M('article')->count();
        $page = new \Think\Page($count,10);
        $p = $_GET['p'];
        $show = $page->show();

        $list=M('article')->page($p, 12)->select();

        foreach ($list as $k=> $v) {
           $list[$k]['type_id']=M('article_type')->field('title')->find($v['type_id']);
           $list[$k]['create_time']=date("Y-m-d");
        }
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }

    //文章分类
    public function type()
    {
        $list=M('article_type')->select();
        $this->assign('list',$list);
        $this->display();
    }

    //添加文章
    public function add_article()
    {   
       
        if(IS_POST)
        {
            $rules = array(
                 array('title','require','标题不能为空',1),
                 array('type_id','require','类型不能为空',1),
                 array('content','require','内容不能为空',1),
            );
            $wxuser = M("article"); 
            if (!$wxuser->validate($rules)->create()){
                echo $wxuser->getError();die;
            }
            $data=I('post.');
            $data['create_time']=time();
            $data['content']=$data['content'];
            $res=M('article')->add($data);

            if(!$res)
            {
                echo 1;die;
            }
            $typeid=I('type_id');

            if($typeid=='1')
            {   
                $data1['status']='1';
                $users= D('wxuser')->field('uid,status')->select();

                foreach ($users as $k=> $v) {
                    if($v['status']=='2')
                    {
                        $res=M('wxuser')->where('uid='.$v['uid'])->save($data1);
                    }
                }
            }
            echo 0;die; 
        }
        $category=M('article_type')->select();
        $this->assign('category',$category);
        $this->assign('list',$list);
        $this->display();
    }

    //编辑文章
    public function edit_article()
    {   
        $id=I('id');

        if(empty($id))
        {   
            echo "参数错误";die;
        }

        if(IS_POST)
        {
            $rules = array(
             array('title','require','标题不能为空',1),
             array('type_id','require','类型不能为空',1),
             array('content','require','内容不能为空',1),
            );
            $wxuser = M("article"); 
            if (!$wxuser->validate($rules)->create()){
                echo $wxuser->getError();die;
            }
            $data=I('post.');
            $data['content']=$_POST['content'];

            $res=M('article')->save($data);
            if(!$res)
            {
                echo 1;die;
            }
            echo 0;die;
            
        }

        $info=M('article')->find($id);
        if(empty($info)){ echo '找不到该文章';die;}
        $info['content']=$info['content'];
        $category=M('article_type')->select();
        $this->assign('category',$category);
        $this->assign('id',$id);
        $this->assign('info',$info);
        $this->display();
    }

    //添加文章列表
    public function add_type()
    {   

        if(IS_POST)
        {   
            $rules = array(
             array('title','require','标题不能为空',1),
            );
            $wxuser = M("article_type"); 
            if (!$wxuser->validate($rules)->create()){
                echo $wxuser->getError();die;
            }
            $data=I('post.');

            $res=M('article_type')->add($data);
            if(!$res)
            {
                echo 1;die;
            }
            echo 0;die;
        }
        $this->display();
    }

    //编辑文章列表
    public function edit_type()
    {
        $id=I('id');

        if(empty($id))
        {   
            echo "参数错误";die;
        }

        if(IS_POST)
        {
            $rules = array(
             array('title','require','标题不能为空',1),
            );
            $wxuser = M("article"); 
            if (!$wxuser->validate($rules)->create()){
                echo $wxuser->getError();die;
            }
            $data=I('post.');
            $res=M('article_type')->save($data);
            if(!$res)
            {
                echo 1;die;
            }
            echo 0;die;
        }

        $info=M('article_type')->find($id);
        if(empty($info)){echo '找不到该分类';die;}
        $this->assign('id',$id);
        $this->assign('info',$info);
        $this->display();
    }


    //删除文章
    public function del_article()
    {   
        $id=I('id');
        if(empty($id))
        {   
            $this->error('参数错误');
        }

        $info=M('article')->find($id);

        if(empty($info))
        {
            $this->error('找不到该文章！');
        }

        $list=M('article')->delete($id);
        if($list)
        {
            $this->success('删除成功！',u('lst'));
        }
    }

    //删除分类
    public function del_type()
    {   
       $id=I('id');
        if(empty($id))
        {   
            $this->error('参数错误');
        }

        $info=M('article_type')->find($id);

        if(empty($info))
        {
            $this->error('找不到该分类！');
        }

        $list=M('article_type')->delete($id);
        if($list)
        {
            $this->success('删除成功！',u('type'));
        }
    }

   public function SecurityEditorHtml($str) {
    $farr = array(
        "/\s+/", //过滤多余的空白 
        "/<(\/?)(script|i?frame|style|html|body|title|link|meta|\?|\%)([^>]*?)>/isU",
        "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU"
    );
    $tarr = array(
        " ",
        "＜\\1\\2\\3＞",
        "\\1\\2",
    );
    $str = preg_replace($farr, $tarr, $str);
    return $str;
}
}
